using Furion.RemoteRequest.Extensions;
using Grpc.Core;
using Mapster;
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;
using WeChatV3Pay;
using WeChatV3Pay.Application.Refunds.Services;
using WeChatV3Pay.Application.Transactions.Dtos.JsApi;
using WeChatV3Pay.Application.Transactions.Services;

namespace WeChatV3Pay.Services
{
    public class WeChatPayService : WeChatPay.WeChatPayBase
    {
        private readonly ILogger<WeChatPayService> _logger;
        private readonly ITransactionsService _transactionsService;
        private readonly IRefundsService _refundsService;
        public WeChatPayService(ILogger<WeChatPayService> logger, ITransactionsService transactionsService, IRefundsService refundsService)
        {
            _logger = logger;
            _transactionsService = transactionsService;
            _refundsService = refundsService;
        }

        public override async Task<WeixinJSBridgeResponse> JsApiPay(JsApiPayRequest request, ServerCallContext context)
        {
            try
            {
                var req = request.Adapt<JsApiPayReq>();
                var jsApiResponse = await _transactionsService.JsApiPayAsync(req);
                return jsApiResponse.Adapt<WeixinJSBridgeResponse>();
            }
            catch (Exception ex)
            {
                return new WeixinJSBridgeResponse()
                {
                    ErrorMsg = ex.Message,
                };
            }
        }

        public override async Task<QueryTransactionResponse> QueryTransactionsById(QueryTransactionRequest request, ServerCallContext context)
        {
            try
            {
                var req = request.Adapt<QueryTransactionReq>();
                var queryTransactionRes = await _transactionsService.QueryTransactionsByIdAsync(req);
                return queryTransactionRes.Adapt<QueryTransactionResponse>();
            }
            catch (Exception ex)
            {
                return new QueryTransactionResponse()
                {
                    Message = ex.Message,
                };
            }
        }

        public override async Task<QueryTransactionResponse> QueryTransactionsByOutTradeNO(QueryTransactionRequest request, ServerCallContext context)
        {
            try
            {
                var req = request.Adapt<QueryTransactionReq>();
                Console.WriteLine(JsonConvert.SerializeObject(req));
                var queryTransactionRes = await _transactionsService.QueryTransactionsByOutTradeNOAsync(req);
                return queryTransactionRes.Adapt<QueryTransactionResponse>();
            }
            catch (Exception ex)
            {
                return new QueryTransactionResponse()
                {
                    Message = ex.Message,
                };
            }
        }

        public override async Task<RefundsResponse> Refunds(RefundsRequest request, ServerCallContext context)
        {
            try
            {
                var req = request.Adapt<Application.Refunds.Dtos.RefundsReq>();
                Console.WriteLine(JsonConvert.SerializeObject(req));
                var refundsResponse = await _refundsService.RefundsAsync(req);
                return refundsResponse.Adapt<RefundsResponse>();
            }
            catch (Exception ex)
            {
                return new RefundsResponse()
                {
                    Message = ex.Message,
                };
            }
        }
    }
}